package tool.softT

import shared.Pojo.OplsdaSoftT
import tool.Pojo._
import shared.Pojo._
import tool.{Pojo, Tool, plot}
import implicits.Implicits._
import tool.plot.{ModelArg, Oplsda2D, PermuteResult, VPlot}

import java.io.File
import dao._
import play.api.i18n.Messages

/** Created by yz on 7/12/2020
  */
trait OplsdaSoftBT extends AllSoftBT with OplsdaSoftT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    List(
      FileInfoData("OPLSDA_Score.csv", messages("oPLSDAScore")),
      FileInfoData("OPLSDA_Score_2D.pdf", messages("oPLSDAScore2D")),
      FileInfoData("OPLSDA_R2X_R2Y_Q2.csv", messages("oPLSDAR2XR2YQ2")),
      FileInfoData("OPLSDA_R2X_R2Y_Q2.pdf", messages("q2R2Plot")),
      FileInfoData("fitted_curve_parameter.csv", messages("fittedCurveParameter")),
      FileInfoData("OPLSDA_Permutation.csv", messages("oPLSDAPermutation")),
      FileInfoData("OPLSDA_Permutation.pdf", messages("permutationPlot")),
      FileInfoData("OPLSDA_VIP.csv", messages("oPLSDAVIP")),
      FileInfoData("OPLSDA_VPlot.pdf", messages("vipScorePlot")),
      FileInfoData("Diff_Metabolite.csv", messages("diffMetabolite"))
    )
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val command =
      s"""
         |${Tool.rScript} ${new File(
          Tool.rPath,
          "produce_sample_color.R"
        ).unixPath} --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath}
         |${Tool.rScript} ${Tool.rPath.unixPath}/fill_sample_color.R --g ${groupFile.unixPath}  --solution ${solutionFile.unixPath} --pc ${plot.Oplsda2D.plotO.configFileName}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "oplsda_calculate.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "oplsda_volcano_calculate.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "oplsda_permutation_calculate.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "oplsda_arg_calculate.R").unixPath}
           """.stripMargin
    val plotCommands = List(
      plot.Oplsda2D.getCommand(workspaceDir, idStr = ""),
      plot.ModelArg.getCommand(workspaceDir, idStr = ""),
      plot.PermuteResult.getCommand(workspaceDir, idStr = ""),
      plot.VPlot.getCommand(workspaceDir, idStr = "")
    )
    List(CommandData(workspaceDir, List(command) ::: plotCommands))
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
    plot.Oplsda2D.initConfigFile(workspaceDir,idStr = "")
    plot.ModelArg.initConfigFile(workspaceDir,idStr = "")
    plot.PermuteResult.initConfigFile(workspaceDir,idStr = "")
    plot.VPlot.initConfigFile(workspaceDir,idStr = "")
  }

}
